package com.ruoyi.project.modules.domainInfo.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.project.modules.domainInfo.domain.DomainObjectInfo;
import com.ruoyi.project.modules.domainInfo.mapper.DomainInfoMapper;
import com.ruoyi.project.modules.domainInfo.domain.DomainInfo;
import com.ruoyi.project.modules.domainInfo.service.IDomainInfoService;
import com.ruoyi.common.core.text.Convert;

/**
 * 归属域信息Service业务层处理
 * 
 * @author smallrain
 * @date 2022-03-26
 */
@Service
public class DomainInfoServiceImpl implements IDomainInfoService 
{
    @Autowired
    private DomainInfoMapper domainInfoMapper;

    /**
     * 查询归属域信息
     * 
     * @param id 归属域信息主键
     * @return 归属域信息
     */
    @Override
    public DomainInfo selectDomainInfoById(Long id)
    {
        return domainInfoMapper.selectDomainInfoById(id);
    }

    /**
     * 查询归属域信息列表
     * 
     * @param domainInfo 归属域信息
     * @return 归属域信息
     */
    @Override
    public List<DomainInfo> selectDomainInfoList(DomainInfo domainInfo)
    {
        return domainInfoMapper.selectDomainInfoList(domainInfo);
    }

    /**
     * 新增归属域信息
     * 
     * @param domainInfo 归属域信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDomainInfo(DomainInfo domainInfo)
    {
        domainInfo.setCreateTime(DateUtils.getNowDate());
        int rows = domainInfoMapper.insertDomainInfo(domainInfo);
        insertDomainObjectInfo(domainInfo);
        return rows;
    }

    /**
     * 修改归属域信息
     * 
     * @param domainInfo 归属域信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDomainInfo(DomainInfo domainInfo)
    {
        domainInfo.setUpdateTime(DateUtils.getNowDate());
        domainInfoMapper.deleteDomainObjectInfoByDomainId(domainInfo.getId());
        insertDomainObjectInfo(domainInfo);
        return domainInfoMapper.updateDomainInfo(domainInfo);
    }

    /**
     * 批量删除归属域信息
     * 
     * @param ids 需要删除的归属域信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDomainInfoByIds(String ids)
    {
        domainInfoMapper.deleteDomainObjectInfoByDomainIds(Convert.toStrArray(ids));
        return domainInfoMapper.deleteDomainInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除归属域信息信息
     * 
     * @param id 归属域信息主键
     * @return 结果
     */
    @Override
    public int deleteDomainInfoById(Long id)
    {
        domainInfoMapper.deleteDomainObjectInfoByDomainId(id);
        return domainInfoMapper.deleteDomainInfoById(id);
    }

    @Override
    public DomainObjectInfo selectDomainObjectById(Long domainObjectId) {
        return this.domainInfoMapper.selectDomainObjectById(domainObjectId);
    }

    /**
     * 新增归属域对象信息信息
     * 
     * @param domainInfo 归属域信息对象
     */
    public void insertDomainObjectInfo(DomainInfo domainInfo)
    {
        List<DomainObjectInfo> domainObjectInfoList = domainInfo.getDomainObjectInfoList();
        Long id = domainInfo.getId();
        if (StringUtils.isNotNull(domainObjectInfoList))
        {
            List<DomainObjectInfo> list = new ArrayList<DomainObjectInfo>();
            for (DomainObjectInfo domainObjectInfo : domainObjectInfoList)
            {
                domainObjectInfo.setDomainId(id);
                list.add(domainObjectInfo);
            }
            if (list.size() > 0)
            {
                domainInfoMapper.batchDomainObjectInfo(list);
            }
        }
    }
}
